<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

$cur_foot = 'mysetting';
check_login();
$pagetitle = "我的团";

/**
可使用 1
未激活 2
已过期 3
**/

$state = isset($_GET['state']) ? intval($_GET['state']) : 1;

$condition = array( 'uid' => $login_user_id);

if($state == 1)
{
	$condition[] = ' consume ="N" and begin_time<'.time().' and  end_time >'.time();
} 
else if($state == 2)
{
	$condition[] = ' consume ="Y" and begin_time > '.time();
} 
else if($state == 3)
{
	$condition[] = ' consume ="Y" ||  end_time <'.time();
}

$count = Table::Count('card', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 10);

$orders = DB::LimitQuery('card', array(
	'condition' => $condition,
	'order' => 'ORDER BY addtime DESC',
	'size' => $pagesize,
	'offset' => $offset,
));


if(isset($_GET['page'])  && intval($_GET['page']) > 1)
{
	$s_html = '';
	$result = array('ret'=>1,'offset'=>$offset);
	if(!empty($orders)){
		$result['ret'] = 0;
	}
	foreach($orders as $index =>$one)
	{
		$s_html .= '<div class="coup_one" id="'.$one['id'].'">';
		$s_html .= '	<img src="/images/liofis_quan.jpg" class="coup_img" />';
		$s_html .= '	<div class="coup_tit">'.$INI['system']['sitename'].'</div>';
		$s_html .= '	<div class="coup_text_left1">';
		$s_html .= '		<b>'.$one['credit'].'元红包券</b>';
		$s_html .= '		<div class="text_copus_desc1">';
		$s_html .= '		优质水果，新鲜直送～</div>';
		$s_html .= '	</div>';
		$s_html .= '	<div class="coup_time_right1">';
		$s_html .= '		<b>有效期</b>';
		$s_html .= '		<div class="text_copus_time1">';
		$s_html .= '		'.date('Y-m-d',$one['begin_time']).'<br>'.date('Y-m-d',$one['end_time']).'';
		$s_html .= '		</div>';
		$s_html .= '	</div>';
		$s_html .= '</div>';
	}
	
	$result['html'] = $s_html;
	echo json_encode($result);
	die();
}

if(isset($INI['system']['secsite']) && !empty($INI['system']['secsite']))
	$cur_url = $INI['system']['wwwprefix'].$INI['system']['secsite'].'account/cards.php?state='.$state;
else 
	$cur_url = $INI['system']['wwwprefix'].'account/cards.php?state='.$state;



include template('card_index');
